package com.lightkits.wms.domain.deliveryorder;

import com.lightkits.framework.common.exception.LightKitsBizException;
import com.lightkits.framework.common.model.IResult;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.framework.soa.hzero.executor.ServiceExecutor;
import com.lightkits.wms.domain.deliveryorder.service.DeliveryExeDomain;
import com.lightkits.wms.dto.deliveryorder.ExecuteDeliveryRequestType;
import com.lightkits.wms.dto.deliveryorder.ExecuteDeliveryResponseType;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * ExeDeliveryOrderExecutor
 *
 * @author jing.wei@hand-china.com
 * Created on 2019/2/26 17:37
 */
@Component
public class ExeDeliveryOrderExecutor implements ServiceExecutor<ExecuteDeliveryRequestType, ExecuteDeliveryResponseType> {
    private final DeliveryExeDomain deliveryExeDomain;

    @Autowired
    public ExeDeliveryOrderExecutor(DeliveryExeDomain deliveryExeDomain) {
        this.deliveryExeDomain = deliveryExeDomain;
    }

    @Override
    public ExecuteDeliveryResponseType service(ExecuteDeliveryRequestType request) {
        ExecuteDeliveryResponseType response = new ExecuteDeliveryResponseType();
        response.setSuccessful(true);
        try {
            IResult result = deliveryExeDomain.insertValue(request.getDeliveryExeDTO());
            if (!result.isSuccessful()) {

                result.fill(response);
            }
        } catch (LightKitsBizException e) {
            response.setSuccessful(false);
            String mes = StringUtils.substringAfterLast(e.getMessage(), ":");
            response.setMessage(ObjectUtil.isNullOrEmpty(mes) ? e.getMessage() : mes);
        } catch (Exception e) {
            response.setSuccessful(false);
            response.setMessage("Execute Delivery Order Error");
        }

        return response;
    }
}
